package com.yumingjaing.thread;

/**
 * @description: 测试流程简述：
 * t1、t2抢占资源o,谁先抢占到就调用wait方法进入等待队列，
 * 导致的结果是t1,t2均进入到等待队列，在主线程sleep时间到后
 * 获得cpu的调度，拿到了资源o,并调用其notifyAll方法将两个线程唤醒，
 * 等待3s后，查询t1,t2线程的状态，发现均销毁。
 *
 * @author: John
 * @version: TestWaiting.java, v1.0 2024年12月15日 06:32
 */
public class TestWaiting {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (o) {
                System.out.println("1------");
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2 = new Thread(() -> {

            synchronized (o) {
                System.out.println("2------");

                try {
                    o.wait();

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(1000);
        System.out.println(t1.getState());
        System.out.println(t2.getState());
        Thread.sleep(5000);
        synchronized (o){
            o.notifyAll();
        }
        Thread.sleep(3000);
        System.out.println(t1.getState());
        System.out.println(t2.getState());
;
    }
}